#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/7/2
# @Author  : LiJun
# @Email   : lijun@tianyancha.com
# @Version : 1.0.0

import re
import os

def get_devices_list():
    # 读取设备 id
    deviceIdList = list()
    device_unauthorized = list()
    try:
        readDeviceId = list(os.popen("adb devices").readlines())
        print(f"ID:{readDeviceId}")
        for i in range(1, len(readDeviceId) - 1):
            # 正则表达式匹配出 id 信息
            # 是否匹配到ip+端口，匹配到则跳过
            re_ip = re.findall(r'^(?:[0-9]{1,3}\.){3}[0-9]{1,3}:\d{0,5}', readDeviceId[i])
            if re_ip:
                continue
            # 匹配非ip的设备
            deviceId = readDeviceId[i].split("\t")[0]
            # 正则表达式匹配出 授权状态 信息
            device_states = re.findall(r"\b\w*$", readDeviceId[i])[0]
            # 过滤掉 未授权的设备
            if device_states != "device":
                device_unauthorized.append(deviceId)
                print(f"未授权的设备列表：{device_unauthorized}")
                continue
            deviceIdList.append(deviceId)
    except Exception as e:
        pass
    return deviceIdList


if __name__ == '__main__':
    print(get_devices_list())
